package com.woniuxy.hotel.room.mapper;

import com.woniuxy.hotel.room.pojo.Room;
import com.woniuxy.hotel.room.pojo.RoomExample;
import com.woniuxy.hotel.room.pojo.RoomExample.Criteria;
import com.woniuxy.hotel.room.pojo.RoomExample.Criterion;

import java.util.List;
import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class RoomSqlProvider {
    
    public String countByExample (RoomExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("room");
        applyWhere(example, false);
        return SQL();
    }
    
    public String deleteByExample (RoomExample example) {
        BEGIN();
        UPDATE("room");
        SET("roomFlag=1");
        applyWhere(example, false);
        return SQL();
    }
    
    public String insertSelective (Room record) {
        BEGIN();
        INSERT_INTO("room");
        
        if (record.getRoomid() != null) {
            VALUES("roomId", "#{roomid,jdbcType=INTEGER}");
        }
        
        if (record.getHotelid() != null) {
            VALUES("hotelId", "#{hotelid,jdbcType=INTEGER}");
        }
        
        if (record.getRoomnum() != null) {
            VALUES("roomNum", "#{roomnum,jdbcType=INTEGER}");
        }
        
        if (record.getRoomtypeid() != null) {
            VALUES("roomTypeId", "#{roomtypeid,jdbcType=INTEGER}");
        }
        
        if (record.getRoomprice() != null) {
            VALUES("roomPrice", "#{roomprice,jdbcType=DOUBLE}");
        }
        
        if (record.getDiscount() != null) {
            VALUES("discount", "#{discount,jdbcType=INTEGER}");
        }
        
        if (record.getCashpledge() != null) {
            VALUES("cashPledge", "#{cashpledge,jdbcType=INTEGER}");
        }
        
        if (record.getRoomstatus() != null) {
            VALUES("roomStatus", "#{roomstatus,jdbcType=INTEGER}");
        }
        
        if (record.getIdcard() != null) {
            VALUES("idCard", "#{idcard,jdbcType=INTEGER}");
        }
        
        if (record.getRoomtrait() != null) {
            VALUES("roomTrait", "#{roomtrait,jdbcType=LONGVARCHAR}");
        }
        
        return SQL();
    }
    
    public String selectByExampleWithBLOBs (RoomExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("roomId");
        } else {
            SELECT("roomId");
        }
        SELECT("hotelId");
        SELECT("roomNum");
        SELECT("roomTypeId");
        SELECT("roomPrice");
        SELECT("discount");
        SELECT("cashPledge");
        SELECT("roomStatus");
        SELECT("idCard");
        SELECT("roomTrait");
        FROM("room");
        WHERE("roomFlag=0");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }
    
    public String selectByExample (RoomExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("roomId");
        } else {
            SELECT("roomId");
        }
        SELECT("hotelId");
        SELECT("roomNum");
        SELECT("roomTypeId");
        SELECT("roomPrice");
        SELECT("discount");
        SELECT("cashPledge");
        SELECT("roomStatus");
        SELECT("idCard");
        FROM("room");
        WHERE("roomFlag=0");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }
    
    public String updateByExampleSelective (Map<String, Object> parameter) {
        Room record = (Room) parameter.get("record");
        RoomExample example = (RoomExample) parameter.get("example");
        
        BEGIN();
        UPDATE("room");
        
        if (record.getRoomid() != null) {
            SET("roomId = #{record.roomid,jdbcType=INTEGER}");
        }
        
        if (record.getHotelid() != null) {
            SET("hotelId = #{record.hotelid,jdbcType=INTEGER}");
        }
        
        if (record.getRoomnum() != null) {
            SET("roomNum = #{record.roomnum,jdbcType=INTEGER}");
        }
        
        if (record.getRoomtypeid() != null) {
            SET("roomTypeId = #{record.roomtypeid,jdbcType=INTEGER}");
        }
        
        if (record.getRoomprice() != null) {
            SET("roomPrice = #{record.roomprice,jdbcType=DOUBLE}");
        }
        
        if (record.getDiscount() != null) {
            SET("discount = #{record.discount,jdbcType=INTEGER}");
        }
        
        if (record.getCashpledge() != null) {
            SET("cashPledge = #{record.cashpledge,jdbcType=INTEGER}");
        }
        
        if (record.getRoomstatus() != null) {
            SET("roomStatus = #{record.roomstatus,jdbcType=INTEGER}");
        }
        
        if (record.getIdcard() != null) {
            SET("idCard = #{record.idcard,jdbcType=INTEGER}");
        }
        
        if (record.getRoomtrait() != null) {
            SET("roomTrait = #{record.roomtrait,jdbcType=LONGVARCHAR}");
        }
        
        applyWhere(example, true);
        return SQL();
    }
    
    public String updateByExampleWithBLOBs (Map<String, Object> parameter) {
        BEGIN();
        UPDATE("room");
        
        SET("roomId = #{record.roomid,jdbcType=INTEGER}");
        SET("hotelId = #{record.hotelid,jdbcType=INTEGER}");
        SET("roomNum = #{record.roomnum,jdbcType=INTEGER}");
        SET("roomTypeId = #{record.roomtypeid,jdbcType=INTEGER}");
        SET("roomPrice = #{record.roomprice,jdbcType=DOUBLE}");
        SET("discount = #{record.discount,jdbcType=INTEGER}");
        SET("cashPledge = #{record.cashpledge,jdbcType=INTEGER}");
        SET("roomStatus = #{record.roomstatus,jdbcType=INTEGER}");
        SET("idCard = #{record.idcard,jdbcType=INTEGER}");
        SET("roomTrait = #{record.roomtrait,jdbcType=LONGVARCHAR}");
        
        RoomExample example = (RoomExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }
    
    public String updateByExample (Map<String, Object> parameter) {
        BEGIN();
        UPDATE("room");
        
        SET("roomId = #{record.roomid,jdbcType=INTEGER}");
        SET("hotelId = #{record.hotelid,jdbcType=INTEGER}");
        SET("roomNum = #{record.roomnum,jdbcType=INTEGER}");
        SET("roomTypeId = #{record.roomtypeid,jdbcType=INTEGER}");
        SET("roomPrice = #{record.roomprice,jdbcType=DOUBLE}");
        SET("discount = #{record.discount,jdbcType=INTEGER}");
        SET("cashPledge = #{record.cashpledge,jdbcType=INTEGER}");
        SET("roomStatus = #{record.roomstatus,jdbcType=INTEGER}");
        SET("idCard = #{record.idcard,jdbcType=INTEGER}");
        
        RoomExample example = (RoomExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }
    
    public String updateByPrimaryKeySelective (Room record) {
        BEGIN();
        UPDATE("room");
        
        if (record.getHotelid() != null) {
            SET("hotelId = #{hotelid,jdbcType=INTEGER}");
        }
        
        if (record.getRoomnum() != null) {
            SET("roomNum = #{roomnum,jdbcType=INTEGER}");
        }
        
        if (record.getRoomtypeid() != null) {
            SET("roomTypeId = #{roomtypeid,jdbcType=INTEGER}");
        }
        
        if (record.getRoomprice() != null) {
            SET("roomPrice = #{roomprice,jdbcType=DOUBLE}");
        }
        
        if (record.getDiscount() != null) {
            SET("discount = #{discount,jdbcType=INTEGER}");
        }
        
        if (record.getCashpledge() != null) {
            SET("cashPledge = #{cashpledge,jdbcType=INTEGER}");
        }
        
        if (record.getRoomstatus() != null) {
            SET("roomStatus = #{roomstatus,jdbcType=INTEGER}");
        }
        
        if (record.getIdcard() != null) {
            SET("idCard = #{idcard,jdbcType=INTEGER}");
        }
        
        if (record.getRoomtrait() != null) {
            SET("roomTrait = #{roomtrait,jdbcType=LONGVARCHAR}");
        }
        
        WHERE("roomId = #{roomid,jdbcType=INTEGER}");
        
        return SQL();
    }
    
    protected void applyWhere (RoomExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }







}